package com.example.controleautomovel.activity;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.example.controleautomovel.R;
import com.example.controleautomovel.adapter.CombustivelDBAdapter;
import com.example.controleautomovel.pojo.Combustivel;
import com.example.controleautomovel.utils.FieldMoney;

public class NovoAbastecimentoActivity extends Activity {

	EditText txtData;
	FieldMoney txtCustoTotal;
	EditText txtOdometro;
	EditText txtQtdLitros;
	FieldMoney txtValorPorLitro;
	RadioGroup radioCompletoParcial;
	
	LocationManager locationManager = null;
	TextView txtLatitude = null;
	TextView txtLongitude = null;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_novo_abastecimento);
		
		txtData = (EditText) findViewById(R.id.txtData);
		txtCustoTotal = (FieldMoney) findViewById(R.id.txtCustoTotal);
		txtOdometro = (EditText) findViewById(R.id.txtOdometro);
		txtQtdLitros = (EditText) findViewById(R.id.txtQtdLitros);
		txtValorPorLitro = (FieldMoney) findViewById(R.id.txtValorPorLitro);
		radioCompletoParcial = (RadioGroup) findViewById(R.id.radioGroupAbastecimento);
		
		locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
		
		txtLatitude = (TextView) findViewById(R.id.txtLatitude);
		txtLongitude = (TextView) findViewById(R.id.txtLongitude);
		
		Location lastBestLocation = getLastBestLocation();
		txtLatitude.setText("Latitude: " + lastBestLocation.getLatitude());
		txtLongitude.setText("Longitude: " + lastBestLocation.getLongitude());
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.novo_abastecimento, menu);
		return true;
	}
	
	public void salvar(View v){

		String data = txtData.getText().toString();
		Double odometro = new Double(txtOdometro.getText().toString());
		Long qtdLitros = new Long(txtQtdLitros.getText().toString());
		Double valorPorLitro = new Double(txtValorPorLitro.getText().toString());
		int abastecimento = radioCompletoParcial.getCheckedRadioButtonId();
		Double custoTotal = new Double(txtCustoTotal.getText().toString());
		
		txtData.setText("");
		txtCustoTotal.setText("0");
		txtOdometro.setText("");
		txtQtdLitros.setText("");
		txtValorPorLitro.setText("0");
		radioCompletoParcial.clearCheck();
		
		Combustivel c = new Combustivel(data, odometro, valorPorLitro, qtdLitros, custoTotal, abastecimento);
		
		CombustivelDBAdapter db = new CombustivelDBAdapter(this);
		db.open();
		db.inserirCombustivel(c);
		db.close();
		
		finish();
	}
	
	private Location getLastBestLocation() {

		Location locationGPS = this.locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		Location locationNet = this.locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

		long GPSLocationTime = 0;
		if (null != locationGPS) {
			GPSLocationTime = locationGPS.getTime();
		}

		long NetLocationTime = 0;

		if (null != locationNet) {
			NetLocationTime = locationNet.getTime();
		}

		if (0 < GPSLocationTime - NetLocationTime) {
			return locationGPS;
		} else {
			return locationNet;
		}
	}

	public void showTimePickerDialog(View v) {
//		DialogFragment newFragment = new DatePickerFragment();
//	    FragmentManager manager = getFragmentManager();
//		newFragment.show(manager , "datePicker");
	}
	
	

}
